package com.manage.competition.service.impl;

import com.manage.competition.entity.ExaminationQuestions;
import com.manage.competition.mapper.ExaminationQuestionsMapper;
import com.manage.competition.repository.ExaminationQuestionRepository;
import com.manage.competition.service.IExaminationQuestionService;
import com.manage.competition.util.CurrentUserUtil;
import com.manage.competition.util.SnowflakeUtil;
import com.manage.competition.vo.ExaminationQuestionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Create with IDEA
 *
 * @Author: gitee.com/KamisamaXX
 * @Date: Create in 16:51 2019/4/2
 * @Description:
 */
@Service
public class ExaminationQuestionServiceImpl implements IExaminationQuestionService {
    @Autowired
    private ExaminationQuestionsMapper examinationQuestionsMapper;

    @Autowired
    private ExaminationQuestionRepository examinationQuestionRepository;

    @Override
    public int insert(ExaminationQuestions questions) {
        questions.setId(SnowflakeUtil.nextId());
        questions.setCreateUser(CurrentUserUtil.getCurrentUser().getUsername());
        questions.setUpdateUser(CurrentUserUtil.getCurrentUser().getUsername());
        return examinationQuestionsMapper.insert(questions);
    }

    @Override
    public List<ExaminationQuestionVo> findByColumnId(long id) {
        List<ExaminationQuestions> list = examinationQuestionRepository.findByColumnId(id);
        List<ExaminationQuestionVo> voList = new ArrayList<>();
        for(ExaminationQuestions item : list){
            ExaminationQuestionVo vo = new ExaminationQuestionVo();
            BeanUtils.copyProperties(item,vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public int update(ExaminationQuestions questions) {
        questions.setUpdateUser(CurrentUserUtil.getCurrentUser().getUsername());
        return examinationQuestionsMapper.updateByPrimaryKeySelective(questions);
    }

    @Override
    public int delete(ExaminationQuestions questions) {
        return examinationQuestionsMapper.deleteByPrimaryKey(questions.getId());
    }
}
